package code.google.gui.fragments.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import code.google.gui.interfaces.ISearchFragment;
import code.google.model.util.interfaces.ISong;

import com.example.metamusicgui.R;


public class SearchResultAdapter extends BaseAdapter{
	
	private List<? extends ISong> result;
	private LayoutInflater inflater;
	private ISearchFragment fragment;
	
	public SearchResultAdapter(List<? extends ISong> songs, ISearchFragment fragment){
		inflater = (LayoutInflater) fragment.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.result = songs;
		this.fragment = fragment;
	}

	@Override
	public int getCount() {
		if(result == null)
			return 0;
		else
			return result.size();
	}

	@Override
	public Object getItem(int position) {
		if(result == null)
			return null;
		else
			return result.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView( int position, View convertView, ViewGroup parent) {
		
		final ISong song = result.get(position);
		
		View rowView = inflater.inflate(R.layout.search_result_row, parent, false);
		
		TextView songTitle = (TextView) rowView.findViewById(R.id.textview_songtitle);
		TextView songAlbumArtist = (TextView) rowView.findViewById(R.id.textview_songartist_songalbum);
		
		songTitle.setText(song.getSongName());
		songAlbumArtist.setText(song.getArtistName() + " - " + song.getAlbumName());
		
		rowView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//Try to Play
				fragment.dispensePlay(song);
				
			}
		});
		
		return rowView;
	}
	
}
